﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnlineStore.Business_Logic_Layer;
using OnlineStore.Data_Access_Layer;

namespace OnlineStore
{
    public partial class frmExport : Form
    {
        private BizOrder eBizOrder;
        public frmExport()
        {
            InitializeComponent();
            eBizOrder = new BizOrder();
        }
        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                BizOrder eBizOrder = new BizOrder();
                // Get order header by id
                tbOrder eOrder = eBizOrder.GetOrderbyKeyWord("ID", txtOrderID.Text).First();
                // fill data into form.
                List<tbOrderDetail> OrderDetailList = eBizOrder.GetOrderDetailbyOrderID(int.Parse(txtOrderID.Text));
                // fill data into datagrid dgOrdertail
            }
        }

        private void frmExport_Load(object sender, EventArgs e)
        {
            cbKey.Text = "Status";
            txtKeyWord.Text = "Processing";
            LoadOrderList("Status", "Processing");
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            // Check stock on store
            BizExport eBizExport = new BizExport();
            if (eBizExport.ValidateData(dgOrderDetail.Rows))
            {
                if (eBizExport.CheckStockOnStore(dgOrderDetail.Rows)) // true: enough product for sales.
                {
                    // Update status of Order
                    BizOrder eBizOrder = new BizOrder();
                    eBizOrder.UpdateOrderStatus("Delivering", int.Parse(txtOrderID.Text));
                    // Save Export detail
                    eBizExport.SaveExport(int.Parse(txtOrderID.Text), dgOrderDetail.Rows, dCreatedDate.Text.ToString());
                }
            }
        }
        private void KeyWordCheck(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                LoadOrderList(cbKey.Text, txtKeyWord.Text);
            }
        }

        private void LoadOrderList(string strKey, string strValue)
        {            
            List<tbOrder> OrderList = eBizOrder.GetOrderbyKeyWord(strKey, strValue);
            tbOrder eOrder = new tbOrder();
            tbCustomer eCustomer = new tbCustomer();
            dgOrderList.Rows.Clear();
            for (int i = 0; i < OrderList.Count; i++)
            {
                eOrder = OrderList.ElementAt<tbOrder>(i);
                eCustomer = eBizOrder.GetCustomerbyID(eOrder.Customer_ID);

                string[] NewRow = new string[] { eOrder.Order_Id.ToString(), eCustomer.Cus_Name, eCustomer.Address, eCustomer.Phone, eOrder.Created_Date.ToString(), eOrder.Status };
                dgOrderList.Rows.Add(NewRow);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabExport;
            try
            {
                txtOrderID.Text = dgOrderList.Rows[dgOrderList.SelectedCells[0].RowIndex].Cells[0].Value.ToString().Trim();                
                LoadOrder();
            }
            catch (Exception ex)
            {
                ex.ToString();

            }
        }
        private void LoadOrder()
        {
            BizProduct eBizProduct = new BizProduct();
            // Get order header by id
            tbOrder eOrder = eBizOrder.GetOrderbyKeyWord("ID", txtOrderID.Text).First();
            tbCustomer eCustomer = eBizOrder.GetCustomerbyID(eOrder.Customer_ID);
            // fill data into form.
            txtCustomer.Text = eCustomer.Cus_Name;
            txtAddress.Text = eCustomer.Address;            
            txtStatus.Text = eOrder.Status;
            txtDeliveryDate.Text = eOrder.Delivery_Date.ToString();
            // Get order detail
            List<tbOrderDetail> eOrderDetailList = eBizOrder.GetOrderDetailbyOrderID(int.Parse(txtOrderID.Text));
            // fill data into datagrid dgOrdertail
            for (int i = 0; i < eOrderDetailList.Count; i++)
            {
                tbProduct eProduct = eBizProduct.GetProductbyKeyWord("ID", eOrderDetailList[i].Product_ID.ToString()).First();
                string[] NewRow = new string[] { eOrderDetailList[i].Confirm_Qty.ToString(), eOrderDetailList[i].Confirm_Qty.ToString(), eOrderDetailList[i].Product_ID.ToString(), eProduct.Product_Name.ToString(), eProduct.Color.ToString(), eProduct.Size.ToString() };
                dgOrderDetail.Rows.Add(NewRow);
            }
        }

    }
}
